package com.hry.sunflower.service.impl;

import com.hry.sunflower.common.Constant;
import com.hry.sunflower.dao.BaseGpsMapper;
import com.hry.sunflower.pojo.BaseGps;
import com.hry.sunflower.service.BaseGpsService;
import com.hry.sunflower.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BaseGpsServiceImpl implements BaseGpsService {
    // 设置一个计数器
    private static int count;
    // 定义起始经度
    private double beginLng = 113.665841;
    // 定义起始维度
    private double endLat = 34.752457;

    @Autowired
    private BaseGpsMapper baseGpsMapper;

    /**
     * 模拟定位器发送数据并写入数据库中
     */
    @Override
    public void addGpsData() {
        // 模拟定位器发送回来数据写入数据库
        BaseGps baseGps = new BaseGps();
        // 如果 count是20倍数就反向记录一下
        if (count % 20 == 0) {
            beginLng += -Random.random();
            endLat += -Random.random();
            baseGps.setLat(endLat);
            baseGps.setLng(beginLng);
            baseGps.setEmail(Constant.USER_EMAIL);
        } else {
            // 否则就正向记录一下
            beginLng += Random.random();
            endLat += Random.random();
            baseGps.setLat(endLat);
            baseGps.setLng(beginLng);
            baseGps.setEmail(Constant.USER_EMAIL);
        }
        // 写入数据库
        int result = baseGpsMapper.insertSelective(baseGps);
        if (result > 0) {
            count++;
        }
    }

    /**
     * 根据邮箱查询GPS数据
     * @param email
     * @return
     */
    @Override
    public List<BaseGps> getByEmail(String email) {
        return baseGpsMapper.getByEmail(email);
    }
}
